<?php


namespace app\enterprise\controller;

use app\adminapi\controller\AuthController;
use app\model\admin\UserModel;
use app\Request;
use crmeb\services\CacheService;
use crmeb\utils\Arr;
use crmeb\utils\JwtAuth;
use think\facade\Config;
use think\facade\Db;

class Company extends Enterprise
{

    protected function initialize()
    {
        // TODO: Implement initialize() method.
    }


    public function index()
    {
        return '您好！这是一个[enterprise]示例应用';
    }


    public function login()
    {

        $param = $this->request->param();

//        $companyInfo = $this->checkLogin($param['account'], $param['pwd']);

        $companyModel = new UserModel();

        if (!$param['company_id']) {
            return app('json')->fail('请输入公司id');
        }

        if (!$param['company_id'] or !$param['pwd']) {
            return app('json')->fail('请输入账号和密码');
        }

        $companyInfo = $companyModel->where([
                ['company_id', '=', $param['company_id']],
                ['company_name', '=', $param['account']],
                ['status', '=', 1],
            ])
            ->field('id,company_name,company_code,link_tel,address,file,pid, password')
            ->find();

        if (empty($companyInfo) ) {
            return app('json')->fail('账户不存在');
        }

        if ($companyInfo->password != md5($param['pwd'])) {
            return app('json')->fail('密码不正确');
        }

        /** @var JwtAuth $jwtAuth */
        $jwtAuth = app()->make(JwtAuth::class);

        $tokenInfo = $jwtAuth->createToken($companyInfo->id, 'company', ['pwd' => md5($param['pwd'])]);
        $token = $tokenInfo['token'];
        $data = [];
        $data['token'] = $token;
        $data['expires_time'] = strtotime("+ 1 month",time());
        $companyInfo = $companyInfo->hidden(['password']);
        $companyInfo->account = $companyInfo->company_name;
        $companyInfo->level = 0;
        $companyInfo->real_name = $companyInfo->company_name;
        $companyInfo->head_pic = '';


        $field = ['id', 'menu_name', 'icon', 'pid', 'sort', 'menu_path', 'is_show', 'header', 'is_header', 'is_show_path', 'is_show'];

        $menusList = Db::table('sj_company_menu')->field($field)->order('sort DESC,id DESC')->failException(false)->select()->toArray();
        $data['menus'] = Arr::getMenuIviewList($menusList) ;


        $data['user_info'] = $companyInfo;

        $data['unique_auth'] = ['admin-product'];
        $data['logo'] = '';
        $data['logo_square'] = '';
        $data['version'] = '';
        $data['newOrderAudioLink'] = '';
        $data['queue'] = false;
        $data['timer'] = false;
        $data['site_name'] = '';
        $data['site_func'] = ['seckill', 'bargain', 'combination'];

//        return app('json')->success('登录成功', ['token' => $token,'expires_time' => strtotime("+ 1 month",time()),'userInfo' => $companyInfo]);

        return app('json')->success('登录成功', $data);
    }

    public function checkLogin($company_name,$password)
    {

        $companyModel = new UserModel();

        return $companyModel->where(['company_name' => $company_name,'password' => md5($password)])->where('status','=',1)->field('id,company_name,company_code,link_tel,address,file,pid')->find();
    }

    /**
     * 退出登录
     * @param Request $request
     * @return mixed
     */
    public function logout(Request $request)
    {
        $key = trim(ltrim($request->header(Config::get('cookie.token_name')), 'Bearer'));
        CacheService::delete(md5($key));
        return app('json')->success(410002);
    }
}